package com.snail.billing.session;

import java.util.List;

import org.apache.http.NameValuePair;

import com.snail.billing.DataCache;
import com.snail.billing.net.BillingArrayHttpSession;
import com.snail.billing.net.BillingEncode;
import com.snail.billing.net.BillingSecurity;
import com.snail.billing.util.BillingUtil;

public class SdoRegisterSession extends BillingArrayHttpSession {

	public SdoRegisterSession(String account, String pwd) {
		DataCache cache = DataCache.getInstance();
		String host = cache.importParams.hostBusiness;
		String address = String.format("http://%s/ws/register.do?method=sdoRegisterPassport", host);
		setAddress(address);
		addBillingPair("userName", BillingEncode.enCode(account, "GBK"));
		addBillingPair("pwd", pwd);
		addBillingPair("email", "woniu@woniu.woniu");
		addBillingPair("gid", cache.importParams.gameId);
		addBillingPair("rid", "0");
		addBillingPair("sid", "0");
		addBillingPair("ip", BillingUtil.getLocalIp());
		addBillingPair("pid", "7");
		addBillingPair("spreaderId", "0");
		buildParamPair();
	}

	@Override
	public void buildParamPair() {
		BillingSecurity security = getSecurity();
		addParamPair("accessId", security.accessId);
		addParamPair("accessPasswd", security.accessPasswd);
		addParamPair("accessType", security.accessType);
		for (NameValuePair pair : getBillingPairList()) {
			addParamPair(pair.getName(), pair.getValue());
		}
		addParamPair("sVerifyStr", buildVerifyStr(security, getBillingPairList()));
	}

	protected String buildVerifyStr(final BillingSecurity security, final List<NameValuePair> list) {
		StringBuilder builder = new StringBuilder();
		builder.append(security.accessId);
		builder.append(security.accessPasswd);
		builder.append(security.accessType);
		for (NameValuePair pair : list) {
			builder.append(pair.getValue());
		}
		builder.append(security.seed);
		String origin = builder.toString();
		return BillingEncode.MD5(origin);
	}

	@Override
	public BillingSecurity getSecurity() {
		if (DataCache.getInstance().isSandbox) {
			return BillingSecurity.securitySandbox2;
		}
		return BillingSecurity.security;
	}

}
